import createAxios from '/@/utils/axios'
import { useUserInfo } from '/@/stores/userInfo'

export const userUrl = '/api/user/'
export const accountUrl = '/api/account/'

export function checkIn(method: 'get' | 'post', params: object = {}) {
    return createAxios({
        url: userUrl + 'checkIn',
        data: params,
        method: method,
    })
}

export function overview() {
    return createAxios({
        url: accountUrl + 'overview',
        method: 'get',
    })
}

export function postProfile(params: anyObj) {
    return createAxios(
        {
            url: accountUrl + 'profile',
            method: 'POST',
            data: params,
        },
        {
            showSuccessMessage: true,
        }
    )
}

export function getProfile() {
    return createAxios({
        url: accountUrl + 'profile',
        method: 'get',
    })
}

export function postVerification(data: anyObj) {
    return createAxios({
        url: accountUrl + 'verification',
        method: 'post',
        data: data,
    })
}

export function postChangeBind(data: anyObj) {
    return createAxios(
        {
            url: accountUrl + 'changeBind',
            method: 'post',
            data: data,
        },
        {
            showSuccessMessage: true,
        }
    )
}

export function changePassword(params: anyObj) {
    return createAxios(
        {
            url: accountUrl + 'changePassword',
            method: 'POST',
            data: params,
        },
        {
            showSuccessMessage: true,
        }
    )
}

export function getBalanceLog(page: number, pageSize: number) {
    return createAxios({
        url: accountUrl + 'balance',
        method: 'GET',
        params: {
            page: page,
            limit: pageSize,
        },
    })
}

export function getIntegralLog(page: number, pageSize: number) {
    return createAxios({
        url: accountUrl + 'integral',
        method: 'GET',
        params: {
            page: page,
            limit: pageSize,
        },
    })
}

//游戏部分
export function getGameLog(page: number, pageSize: number, select?: string, sortField: string = 'create_time', sortOrder: string = 'desc') {
    const params: any = {
        page: page,
        limit: pageSize,
        sortField: sortField,
        sortOrder: sortOrder,
    }

    // 只有当 select 有值时才传递
    if (select && select !== '') {
        params.select = select
    }

    console.log('getGameLog 请求参数:', params)

    return createAxios({
        url: accountUrl + 'game',
        method: 'GET',
        params: params,
    })
}

export function postLogout() {
    const userInfo = useUserInfo()
    return createAxios({
        url: userUrl + 'logout',
        method: 'POST',
        data: {
            refreshToken: userInfo.getToken('refresh'),
        },
    })
}

export function retrievePassword(params: anyObj) {
    return createAxios(
        {
            url: accountUrl + 'retrievePassword',
            method: 'POST',
            data: params,
        },
        {
            showSuccessMessage: true,
        }
    )
}

export function getPlayerNickname(username: string) {
    return createAxios({
        url: accountUrl + 'getPlayerNickname',
        method: 'GET',
        params: { username },
    })
}

/**
 * 批量获取玩家昵称
 * @param usernames 用户名数组
 */
export function getBatchNicknames(usernames: string[]) {
    return createAxios({
        url: accountUrl + 'getBatchNicknames',
        method: 'GET',
        params: {
            usernames: usernames.join(','),
        },
    })
}

/**
 * 自主查询玩家信息
 * @param username 用户名
 */
export function queryPlayerInfo(username: string) {
    return createAxios({
        url: accountUrl + 'queryPlayerInfo',
        method: 'GET',
        params: { username },
    })
}

export function notice(page: number, pageSize: number) {
    return createAxios({
        url: accountUrl + 'notice',
        method: 'GET',
        params: {
            page: page,
            limit: pageSize,
        },
    })
}
